#include <iostream>
#include <string>
#include <vector>
using namespace std;
void constructor()
{
	vector <int> v;
	for (int i = 0; i < 10; ++i)
	{
		v.push_back(i);
	}
	for(vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}
	cout<<endl;

	vector<int>w(v.begin(), v.end());
	for(vector<int>::iterator i = w.begin(); i != w.end(); ++i)
	{
		cout<<*i<<"\t";
	}
	cout<<endl;

	vector<int>x(10, 200);
	for (std::vector<int>::iterator i = x.begin(); i != x.end(); ++i)
	{
		cout<<*i<<"\t";
	}
	cout<<endl;

	vector<int>y(x);
	for (std::vector<int>::iterator i = y.begin(); i != y.end(); ++i)
	{
		cout<<*i<<"\t";
	}
	cout<<endl<<endl;
}
void assignment()
{
	vector <int> v;
	for (int i = 1; i <= 10; ++i)
	{
		v.push_back(i*10);
	}
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}
	cout<<endl;

	vector <int> w;
	w = v;
	for (std::vector<int>::iterator i = w.begin(); i != w.end(); ++i)
	{
		cout<<*i<<"\t";
	}
	cout<<endl;

	vector <int> x;
	x.assign(v.begin(), v.end());
	for (std::vector<int>::iterator i = x.begin(); i != x.end(); ++i)
	{
		cout<<*i<<"\t";
	}
	cout<<endl;

	vector <int> y;
	y.assign(10, 200);
	for (std::vector<int>::iterator i = y.begin(); i != y.end(); ++i)
	{
		cout<<*i<<"\t";
	}
	cout<<endl<<endl;
}
void operation()
{
	vector <int> v;
	v.assign(10, 200);
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}
	cout<<endl;

	cout<<"Is v NULL?:\t"<<v.empty()<<endl;
	cout<<"Capacity of v:\t"<<v.capacity()<<endl;
	cout<<"Size of v:\t"<<v.size()<<endl;
	v.resize(15);
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl;
	v.resize(20, 300);
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl<<endl;
	v.resize(5);
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl;
}
void insertDelete()
{
	vector <int> v;
	for (int i = 1; i <= 10; ++i)
	{
		v.push_back(i*10);
	}
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl;
	v.pop_back();
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl;
	v.insert(v.begin(), 100);
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl;
	v.insert(v.begin(), 2, 200);
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl;
	v.erase(v.begin());
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl;
	v.erase(v.begin(), v.end());
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<"NULL"<<endl;
	v.clear();
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<"NULL"<<endl;
}
void depositFetch()
{
	vector <int> v;
	for (int i = 1; i <= 10; ++i)
	{
		v.push_back(i*10);
	}
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl<<v[1]<<"\t"<<v.at(2)<<endl;
	cout<<v.front()<<"\t"<<v.back()<<endl;
}
void exchange()
{
	vector <int> v;
	for (int i = 1; i <= 10; ++i)
	{
		v.push_back(i*10);
	}cout<<"exchange before"<<endl;
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl;

	vector <int> w;
	for (int i = 11; i <= 20; ++i)
	{
		w.push_back(i*10);
	}
	for (std::vector<int>::iterator i = w.begin(); i != w.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl;

	v.swap(w);
	cout<<"exchange after"<<endl;
	for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl;
	for (std::vector<int>::iterator i = w.begin(); i != w.end(); ++i)
	{
		cout<<*i<<"\t";
	}cout<<endl;
}
// example
void exchangeExample()
{
	vector <int> v;
	for (int i = 1; i <= 100000; ++i)
	{
		v.push_back(i*10);
	}
	cout<<"capacity:\t"<<v.capacity()<<endl;
	cout<<"size:\t"<<v.size()<<endl;
	v.resize(3);
	cout<<"compression before"<<endl;
	cout<<"capacity:\t"<<v.capacity()<<endl;
	cout<<"size:\t"<<v.size()<<endl;
	vector<int>(v).swap(v);
	cout<<"compression after"<<endl;
	cout<<"capacity:\t"<<v.capacity()<<endl;
	cout<<"size:\t"<<v.size()<<endl;
}
void reserve()
{
	vector<int> v;
	// 利用reserve预留空间
	v.reserve(10000000);
	int n = 0;
	int * p = NULL;
	for (int i = 0; i < 10000000; ++i)
	{
		v.push_back(i);
		if(p != &v[0])
		{
			p = &v[0];
			n++;
		}
	}
	cout<<v.capacity()<<endl;
	cout<<v.size()<<endl;
	cout<<n<<endl;
}
int main(int argc, char const *argv[])
{
	// constructor();
	// assignment();
	// operation();
	// insertDelete();
	// depositFetch();
	// exchange();
	// exchangeExample();
	reserve();
	return 0;
}